package com.hzt.agprosaleplatform.controller;

import com.hzt.agprosaleplatform.common.CustomResponse;
import com.hzt.agprosaleplatform.entity.Address;
import com.hzt.agprosaleplatform.entity.Shop;
import com.hzt.agprosaleplatform.entity.User;
import com.hzt.agprosaleplatform.service.ShopService;
import com.hzt.agprosaleplatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:5173")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    ShopService shopService;

    @PostMapping("/user/baseInfo/update")
    public CustomResponse updateUserBaseInfo(@RequestBody User user) {
        try {
            boolean success = userService.updateUserInfo(user);
            if (success) {
                return new CustomResponse(200, "个人信息更新成功", null);
            } else {
                return new CustomResponse(500, "个人信息更新失败", null);
            }
        }catch (Exception e) {
            return new CustomResponse(500, "个人信息更新失败", null);
        }
    }

    //测试上传头像至OSS
    @PostMapping("/user/avatar/update")
    public CustomResponse updateUserAvatar(@RequestPart("uid") String uid, @RequestPart("file") MultipartFile file) {

        System.out.println(file.getOriginalFilename());
        try{
            System.out.println("接收到的uid: " + uid);
            System.out.println("接收到的文件名: " + file.getOriginalFilename());

            return userService.updateUserAvatar(uid, file);
        }catch (Exception e){
            e.printStackTrace();
            return new CustomResponse(500,"头像更新失败",null);
        }
    }

    /**
     * 更新尼玛接口
     * @param uid
     * @param password
     * @return
     */
    @PutMapping("/user/NewPassword")
    public CustomResponse updateUserPassword(@RequestParam String uid,@RequestParam String password) {
        try{
            return userService.updateUserPassword(uid, password);
        }catch (Exception e){
            return new CustomResponse(500,"密码更新失败",null);
        }
    }

    //User地址新增
    @PostMapping("/user/address/new")
    public CustomResponse addNewAddress(@RequestBody Address address) {
        System.out.println("当前添加新地址用户:" + address.getUid());
        try{
            return userService.addReceivedAddress(address);
        }catch (Exception e){
            return new CustomResponse(500,"新增地址失败",null);
        }
    }

    //获取当前uid下的全部地址
    @GetMapping("/user/address/list")
    public CustomResponse userAddressList(@RequestParam String uid) throws IOException {
        try {
            List<Address> userAddressList = userService.UserAddressList(uid);

            return new CustomResponse(200,"OK",userAddressList);
        }catch (Exception e){
            return new CustomResponse(500,"ERROR",null);
        }
    }


    //修改传入的地址实体，进行修改再存入
    @PutMapping("/user/address/update")
    public CustomResponse userUpdateAddress(@RequestBody Address address) {
        try{
            return userService.updateReceivedAddress(address);
        } catch (IOException e) {
            return new CustomResponse(500,"地址更新失败",null);
        }
    }


    @DeleteMapping("/user/address/{aid}")
    public CustomResponse userDeleteAddress(@PathVariable String aid) {
        try {
            return userService.deleteReceivedAddress(aid);
        } catch (Exception e) {
            e.printStackTrace();
            return new CustomResponse(500, "删除地址失败：" + e.getMessage(), null);
        }
    }


    @PostMapping("/user/openShop")
    public CustomResponse CreateUserShop(@RequestBody Shop shop) {
        try {
            return shopService.CreateShop(shop);
        }catch (Exception e){
            return new CustomResponse(400,"每位用户只能注册一个店铺",null);
        }
    }

    /**
     * 根据Uid查询商铺
     * @param uid
     * @return
     */
    @GetMapping("/user/shopByUid/{uid}")
    public CustomResponse UserShopByUid(@PathVariable int uid){
        try{
            Shop shop = shopService.selectShopByUId(uid);
            return new CustomResponse(200,"OK",shop);
        }catch (Exception e){
            return new CustomResponse(500,"查询失败",null);
        }
    }

    /**
     * 根据shopID查询店铺
     * @param shopid
     * @return
     */
    @GetMapping("/user/shopByShopId/{shopid}")
    public CustomResponse UserShopByShopId(@PathVariable int shopid){
        try{
            Shop shop = shopService.selectShopByShopID(shopid);
            return new CustomResponse(200,"OK",shop);
        }catch (Exception e){
            return new CustomResponse(500,"ERROR",null);
        }
    }


    @PostMapping("/user/shop/update")
    public CustomResponse UserShopUpdate(@RequestBody Shop shop) {
        try{
            return shopService.UpdateShopInfo(shop);
        }catch (Exception e){
            return new CustomResponse(500,"店铺信息更新失败",null);
        }
    }

}